// iViewComponent/address-textarea/index.js
Component({
    behaviors: ['wx://form-field'],
    /**
     * 组件的属性列表
     */
    properties: {
        title: {
            type: String
        },

        disabled: {
            type: Boolean,
            value: false
        },
        placeholder: {
            type: String,
            value: ''
        },
        autofocus: {
            type: Boolean,
            value: false
        },

        right: {
            type: Boolean,
            value: false
        },
        error: {
            type: Boolean,
            value: false
        },
        maxlength: {
            type: Number
        },
        autoHeight: {
            type: Boolean,
            value: true
        },
        key: {
            type: String
        },
        isMust: {
            type: Boolean,
            value: true
        },
        isReplaceEmoji: {
          type: Boolean,
          value: false
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        showModal: false,
        modalActions: [{
                name: '拒绝授权'
            },
            {
                name: '立即授权',
                openType: 'openSetting',
                color: '#2d8cf0',
            }
        ],
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /**打开地图组件选择地址 */
        selectLocation() {
            // console.log('selectLocation!!')
            wx.chooseLocation({
                success: (res) => {
                    console.log(res)
                    this.setData({
                        value: res.address
                    })
                    let param = {
                        key: this.data.key,
                        value: res.address
                    }
                    this.triggerEvent('change', param);
                },
                fail: res => {
                    console.log('fail', res)
                    wx.getSetting({
                        success: (res) => {
                            if (!res.authSetting['scope.userLocation']) {
                                this.setData({
                                    showModal: true
                                })
                            }
                        }
                    })
                },
                complete: res => {
                    console.log('complete', res)
                }
            })
        },
        handleInputChange(v) {
            console.log('change', v)
            let currentValue = v.detail.value;
            if(this.data.isReplaceEmoji){
              if (v || v != '') {
                let reg = /([^\u0020-\u007E\u00A0-\u00BE\u2E80-\uA4CF\uF900-\uFAFF\uFE30-\uFE4F\uFF00-\uFFEF\u0080-\u009F\u2000-\u201f\u2026\u2022\u20ac\r\n])/g;
                currentValue = currentValue.replace(reg, '');
              }
            }
            this.setData({
              value: currentValue
            })
            let param = {
                key: this.data.key,
              value: currentValue
            }
            this.triggerEvent('change', param);
        },

        /**
         * 处理弹出框的按钮点击事件
         * @param {*} e 
         */
        handleModalActionClick(e) {
            this.setData({
                showModal: false
            })
        },
    }
})